# -*- coding: utf-8 -*-

import math

from utils import k_util, parse_util


# 求股票的标准差
def stock_standard_deviation(code, size):
    return standard_deviation(stock_closes(code, size))


# 求股票的离散系数
def stock_discrete_value(code, size):
    return discrete_value(stock_closes(code, size))


def mean(x):
    return sum(x) / len(x)


# 计算每一项数据与均值的差
def de_mean(x):
    x_bar = mean(x)
    return [x_i - x_bar for x_i in x]


# 辅助计算函数 dot product 、sum_of_squares
def dot(v, w):
    return sum(v_i * w_i for v_i, w_i in zip(v, w))


def sum_of_squares(v):
    return dot(v, v)


# 方差
def variance(x):
    n = len(x)
    deviations = de_mean(x)
    return sum_of_squares(deviations) / (n - 1)


# 标准差
def standard_deviation(x):
    return math.sqrt(variance(x))


# 协方差
def covariance(x, y):
    n = len(x)
    return dot(de_mean(x), de_mean(y)) / (n - 1)


# 相关系数
def correlation(x, y):
    stdev_x = standard_deviation(x)
    stdev_y = standard_deviation(y)
    if stdev_x > 0 and stdev_y > 0:
        return covariance(x, y) / stdev_x / stdev_y
    else:
        return 0


# 均方根值
def root_mean_square(x):
    return math.sqrt(sum([x_ ** 2 for x_ in x]) / len(x))


# 离散系数
def discrete_value(x):
    if len(x) == 0:
        return 1
    if len(x) == 1:
        return 0
    return standard_deviation(x) / mean(x)
    # = 标椎差/平均值


# 求股票收盘价格列表
def stock_closes(code, size):
    day_ks = k_util.day_k_local(code, size)
    closes = []
    for infos in day_ks:
        infos = parse_util.p_dict(infos)
        closes.append(float(infos['sp']))
    return closes


# 求[区间]股票收盘价格列表
def stock_interval_closes(code, beg, size):
    day_ks = k_util.day_k_local(code, beg + size)
    closes = []
    count = 0
    for infos in day_ks:
        count += 1
        if count < beg:
            continue
        infos = parse_util.p_dict(infos)
        closes.append(float(infos['sp']))
    return closes


if __name__ == '__main__':
    # stock_info = select_util.select_doc()
    # s_ds = {}
    # for code in stock_info:
    #     s_d = stock_standard_deviation(code, 100)
    #     s_ds[code] = s_d
    #     print(s_d)
    # print(s_ds)

    # values = []
    # standards = []
    # variances = []
    # for i in range(10):
    #     x = stock_closes('002555', (i + 1) * 10)
    #     values.append(discrete_value(x))
    #     variances.append(variance(x))
    #
    # print(values)
    # print(variances)
    # a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # b = [991, 992, 993, 994, 995, 996, 997, 998, 999]
    # print(discrete_value(a))
    # print(discrete_value(b))
    # print(variance(a))
    # print(variance(b))

    print(correlation([3, 4, 5, 6, 7, 8], [6, 5, 4, 3, 2, 1]))

# a = [random.randint(0, 100) for a in range(20)]
# b = [random.randint(0, 100) for b in range(20)]
# print(a)
# print(b)
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# b = [12, 12, 14, 14, 16, 16, 18, 18, 19]
#
# print(variance(a))
# print(variance(b))
#
# print(covariance(a, b))
#
# # 构造矩阵
# ab = np.array([a, b])
# # 计算协方差矩阵
# print(np.cov(ab))
# # 相关系数
# print(np.corrcoef(ab))
#
# print("11111")
# # 方差
# print(np.var(a, ddof=1))
# # 标准差
# print(np.std(a, ddof=1))
# print(np.std(b, ddof=1))
